﻿/*Page created by Nivasan Pillay -- holds the information for the PrintRecipe page*/

using System;
using Microsoft.Practices.ObjectBuilder;
using Common;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace CSC440TeamOne.MyNutrition.Views
{
	public partial class PrintRecipe : Microsoft.Practices.CompositeWeb.Web.UI.Page, IPrintRecipeView
	{
		private PrintRecipePresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
                this.RecipID = Request.Params["RecipeID"] == null ? int.MinValue : int.Parse(Request.Params["RecipeID"]);

				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();
		}

		[CreateNew]
		public PrintRecipePresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

		// TODO: Forward events to the presenter and show state to the user.
		// For examples of this, see the View-Presenter (with Application Controller) QuickStart:
		//	

        private void populate(Recipe recipe)
        {
            this.name.InnerText = recipe.name;
            this.repIngredients.DataSource = recipe.ingredients;
            this.repIngredients.DataBind();
            this.lblDirections.Text = recipe.directions;
        }

        protected void repIngredients_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, RecipeIngredient>.Bind(e.Item, a =>
            {
                a.Find<Label>("Label1").Text = a.BusinessItem.ToString();
            });
        }

        protected void recipePrint_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, Recipe>.Bind(e.Item, a =>
            {
                a.Find<LinkButton>("recPrint").Text = a.BusinessItem.name;
                a.Find<LinkButton>("recPrint").CommandArgument = a.BusinessItem.RecipeID.ToString();
            });
        }

        /*protected void rodNamePrint_Click(object sender, EventArgs e)
        {
            MyRecipeViewModel mrvm = _presenter.recipeSelected(Convert.ToInt32(this.name.InnerText));
            this.populate(mrvm.selectedRecipe);
        }

        protected void recipePrint_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            MyRecipeViewModel mrvm = _presenter.recipeSelected(Convert.ToInt32(e.CommandArgument));
            this.populate(mrvm.selectedRecipe);
        }*/

        public void Show(MyRecipeViewModel p)
        {
            /*this.rodNamePrint.Text = p.selectedRecipe.name;
            this.rodNamePrint.CommandArgument = p.selectedRecipe.RecipeID.ToString();
            this.populate(p.selectedRecipe);

            this.recipePrint.DataSource = p.selectedRecipe;
            this.recipePrint.DataBind();*/

            this.name.DataBind();
            this.name.InnerText = p.selectedRecipe.name;

            List<RecipeIngredient> ingredients = p.selectedRecipe.ingredients;
            repIngredients.DataSource = ingredients;
            repIngredients.DataBind();

            //this.repIngredients.DataSource = p.selectedRecipe;
            //this.repIngredients.DataBind();

            //this.lblDirections.DataBind();
            this.lblDirections.Text = p.selectedRecipe.directions;
        }

        public void Show(PrintRecipeViewModel p)
        {
            /*this.rodNamePrint.Text = p.selectedRecipe.name;
            this.rodNamePrint.CommandArgument = p.selectedRecipe.RecipeID.ToString();
            this.populate(p.selectedRecipe);

            this.recipePrint.DataSource = p.selectedRecipe;
            this.recipePrint.DataBind();*/

            this.name.DataBind();
            this.name.InnerText = p.selectedRecipe.name;

            List<RecipeIngredient> ingredients = p.selectedRecipe.ingredients;
            repIngredients.DataSource = ingredients;
            repIngredients.DataBind();

            //this.repIngredients.DataSource = p.selectedRecipe;
            //this.repIngredients.DataBind();

            //this.lblDirections.DataBind();
            this.lblDirections.Text = p.selectedRecipe.directions;
        }

        public void update(PrintRecipeViewModel model)
        {
            throw new NotImplementedException();
        }

        public void OnSaveViewCompleted(PrintRecipeViewModel model)
        {
            throw new NotImplementedException();
        }

        public int RecipID
        {
            get
            {
                return ViewState["RecipID"] != null ? (int)ViewState["RecipID"] : int.MinValue;
            }
            set
            {
                ViewState["recipeID"] = value;
            }
        }
    }
}

